//加密平台标识
const OS = 'web';

//加密盐
const SALT = 'emhhHaC';

//上传地址
const UPLOADURL = 'https://api.zhgylgl.com/api/uploadImg';

//获取请求头
function getHeader() {
    const timeStamp = Date.now() + '';
    const sign = '01' + md5(timeStamp + SALT);
    // const token = localStorage.getItem('zhToken') || 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJwaG9uZSI6IjEzMzczNzI3NjE1IiwidXNlcl9pZCI6MzU1NjcsImxldmVsIjozLCJhcHByb3ZlIjowLCJpYXQiOjE3MDk2MjA3OTJ9.U3p7HLmiwUpekvxXacKwm8jIECj3_NPVb9gxeJvqzdc';
    // const userId = localStorage.getItem('zhUserId') || '35567';
    const token = localStorage.getItem('zhToken');
    const userId = localStorage.getItem('zhUserId');
    return {
        'os': OS,
        'x-time': timeStamp,
        'x-sign': sign,
        'token': token,
        'userId': userId
        
    }
}

//初次进入判断是否存在token
function isLogin() {
    // return false
    return localStorage.getItem('zhToken')
}

//登陆后设置登陆状态
function setLoginState(info) {
    localStorage.setItem('zhToken', info.token);
    localStorage.setItem('zhUserId', info.user_id);
    localStorage.setItem('zhUserPhone', info.phone);
}

//重写ajax方法
function request(option) {
    return new Promise((resolve, reject) => {
        $.ajax(
            {
                ...option,
                headers: getHeader(),
                success: (res) => {
                    if (typeof res == 'string') {
                        resolve(JSON.parse(res))
                    } else {
                        resolve(res)
                    }
                },
                error: (e) => {
                    reject(e)
                }
            }
        )
    })
}